<?php
class VcardAction extends CommonAction {
	public $pagezise=10;
	public $listmap=1;
	public $listorder='id';
	
    public function index(){  
	
	if($this->is_weixin()){
		Vendor('Weixin.Weixin');
		$d['token']=getSysVal('token');
		$d['appid']=getSysVal('appid');
		$d['appsecret']=getSysVal('appsecret');
		$this->weixin=new WeixinChat($d);
		//dump($this->weixin->getSignPackageJSSDK()); exit;
 		 $this->assign('signPackage', $this->weixin->getSignPackageJSSDK());
		
		// dump($signPackage); exit; 
		 //$this->weixin=new WeixinChat($d);
		//$this->assign('addressSign_info', $this->weixin->getSignPackage());

		//echo $this->weixin->getSignPackage();exit;
		 
	}
		
	
		if($this->_get('member_id')){ 
		
			$member=$this->_get('member_id');
			$member=$member*1;

		if(!is_int($member)){ exit();}
			
		if($member =='my'){
			if(!$this->memberInfo['id']){redirect(U('Member/Public/login')); exit();}
			else{$member=$this->memberInfo['id']; redirect(U('Vcard/index',array('member_id'=>$member)));exit();}
 	     }
		
		$this->dao = M('Vcard');
		$this->info=M('Info');
		
		//赋予模板初始值 member=1590//为初始模板
		  $smember = 1590;
		  $sinfo = $this->dao -> where(array('member_id' => $smember)) -> find();
		//结束
		
		$this->listmap = "  member_id=".$member." and type='art'";
		
		$info = $this->dao -> where(array('member_id' => $member)) -> find();

		$data = array();
		
		foreach($info as $k => $v){
			
			if($info[$k] == ""){
		       $info[$k] = $sinfo[$k];
			}
		   
		}

		if(!$info){exit();}
		session('ips_id',$info['member_id']);
		$moban=$info['moban'];
        $moban_bg=$info['bg'];
			
		$this->assign('tel',$info['tel']);
		
		
		//if(!$info['tel']){	
		    //$this->listmap = "  member_id=1878 and type='art'";
 		    $info_ = $this->dao -> where(array('member_id' => 1878)) -> find();
 		//}
		
		
		
		foreach($info as $k => $v){
			if($v == ''){
				$info[$k] = $info_[$k];
			}
 		}
		
		//产品表
		 
		import ( 'ORG.Util.Page' );
		
		$this->listmap.=" and type = 'art'";
		
		$count=$this->info->where($this->listmap)->count();
		$p=new Page($count,$this->pagezise);
		$this->list = $this->info->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
	'.$p->listRows)->select();
	
	    $this->listnews = $this -> info -> where(array('site_id' => __SITEID__, 'type' => 'news', 'member_id' => $member)) -> limit('0,10') -> select();
		$this->pages=$p->show();
		
        $this->assign('info_id',$this->memberInfo['id']);
	//微信 
	 	$this->assign('is_weixin',$this->is_weixin());
		$info['bg']=$moban_bg;
		 //大图
 		$info['bg']=explode(',',$info['bg']);
		//end大图
		$this->assign('info',$info);
		$this->assign('session_id',$this->memberInfo['id']);
		$this->display(index.$moban);
		
		}
		 
	}
	
public function down(){
		Vendor('Vcard.VCardIFL'); 
			if($this->_get('member_id')){
				$this->dao = M('Vcard');
				$info = $this->dao -> where(array('member_id' => $this->_get('member_id'))) -> find();
			 error_reporting(0); 
			$arData["fileName"] =  $info['name'];
			$arData["saveTo"] = './vcf';
			$arData["vcard_birtda"] = '';
			$arData["vcard_f_name"] = $info['name'];
			$arData["vcard_s_name"] = '';
			$arData["vcard_uri"] = $info['wz'];
			$arData["vcard_nickna"] = '2';
			$arData["vcard_note"] = '3';
			$arData["vcard_compan"] = $info['company'];
			$arData["vcard_p_pager"] = '';
			$arData["vcard_c_mobile"] = '555';
			$arData["vcard_h_mail"] = $info['email'];
			$arData["vcard_h_phon"] = $info['phone'];
			$arData["vcard_cellul"] = $info['tel'];
			$arData["vcard_h_addr"] = $info['address'];
			$arData["vcard_h_city"] = '';
			$arData["vcard_h_coun"] = '';
			$arData["vcard_h_fax"] = '';
			$arData["vcard_h_zip"] = '';
			$arData["vcard_h_uri"] = '';
			$vcard = new VCardIFL($arData);
			$vcard->createVcard();
			$vcard->SaveVcard();
			$vcard->DownloadVcard();  
			 }
		 }
 
 
 
 //点赞
	public function yesOrNo(){
		if($this->_post('member_id')){
			if(!$this->memberInfo['id']){
				
			   session('jumpurl',$this->_post('jurl'));
 				$data['a']=2;
				echo json_encode($data);exit();
				 
			}
			session('jumpurl',NULL);
			 
			$member_id = $this->_post('member_id');
			$db = M('vcard');
			$emdb = M('vcard_dz');
			$data = $db -> where(array('member_id' => $member_id))-> find();
			$yed = $emdb -> where(array('vid' => $member_id, 'zid' => $this->memberInfo['id']))-> find();
			if(is_array($yed)){
				    $this->error('您已经赞过了');exit();
			}
		 
				$sdata['click'] = $data['click']+1;
				//20150302修改
				//$yes = M('Member') -> where(array('id' => $data['member_id']))->find();//dump($yes);
				//M('Member') -> where(array('id' => $data['member_id'])) ->data(array('yes' => $yes['']+1)) -> save();
				
			 
			$dz=$db -> where(array('member_id' => $member_id))->data($sdata)-> save();
			$ssdata['zid']=$this->memberInfo['id'];
			$ssdata['vid'] = $member_id;
			 
			$dz2=$emdb -> data($ssdata) -> add();
			if($dz >0 and $dz2 >0){
				 
			    $this->success($data['click']+1);exit();
			} 
		}
	}
	
	
	 //收藏
	public function sc(){
		if($this->_post('member_id')){
			if(!$this->memberInfo['id']){
				session('jumpurl',$this->_post('jurl'));
				$data['a']=2;
				echo json_encode($data);exit();
				 
			}
			session('jumpurl',NULL);
			 
			$member_id = $this->_post('member_id');
			$db = M('vcard');
			$emdb = M('vcard_sc');
			$data = $db -> where(array('member_id' => $member_id))-> find();
			$yed = $emdb -> where(array('vid' => $member_id, 'zid' => $this->memberInfo['id']))-> find();
			if(is_array($yed)){
				    $this->error('您已经收藏了');exit();
			}
		 
				$sdata['sc'] = $data['sc']+1;
				//20150302修改
				//$yes = M('Member') -> where(array('id' => $data['member_id']))->find();//dump($yes);
				//M('Member') -> where(array('id' => $data['member_id'])) ->data(array('yes' => $yes['']+1)) -> save();
				
			 
			$dz=$db -> where(array('member_id' => $member_id))->data($sdata)-> save();
			$ssdata['zid']=$this->memberInfo['id'];
			$ssdata['vid'] = $member_id;
			 
			$dz2=$emdb -> data($ssdata) -> add();
			if($dz >0 and $dz2 >0){
				 
			    $this->success($data['sc']+1);exit();
			} 
		}
	}
	
//判断是否为微信
	public function is_weixin(){ 
	if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
		 return true;
	}	
	    return false;
    }
	
	public function show(){
		
	 if($this->_get('member_id')){  
			$member=$this->_get('member_id');
			$member=$member*1;
			if(!is_int($member)){exit();}
		if($member =='my'){
			if(!$this->memberInfo['id']){redirect(U('Member/Public/login')); exit();}
			else{$member=$this->memberInfo['id'];}
 			 }
		
		$this->dao = M('Vcard');
		$this->info=M('Info');
		$this->listmap = "  member_id=".$member." and type='art'";
		
		$info = $this->dao -> where(array('member_id' => $member)) -> find();
		if(!$info){exit();}
		session('ips_id',$info['member_id']);
		$moban=$info['moban'];
		$moban_bg=$info['bg'];
		$this->assign('tel',$info['tel']);
		
		if(!$info['tel']){
 		$member=1878;	
		$this->listmap = "  member_id=".$member." and type='art'";
 		$info = $this->dao -> where(array('member_id' => $member)) -> find();
 		}
		//产品表
		 
		import ( 'ORG.Util.Page' );
		
		$this->listmap.=" and type = 'art'";
		
		$count=$this->info->where($this->listmap)->count();
		$p=new Page($count,$this->pagezise);
		$this->list = $this->info->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
	'.$p->listRows)->select();
	
	    $this->listnews = $this -> info -> where(array('site_id' => __SITEID__, 'type' => 'news', 'member_id' => $member)) -> limit('0,10') -> select();
		$this->pages=$p->show();
		
        $this->assign('info_id',$this->memberInfo['id']);
	//微信 
	 	$this->assign('is_weixin',$this->is_weixin());
		$info['bg']=$moban_bg;
		$this->assign('info',$info);
		
		if($this->_get('type')=='pro'){ 
		$this->display('pro');
		exit;
		}
		if($this->_get('type')=='news'){ 
		$this->display('news');
		exit;
		}
		if($this->_get('type')=='about'){ 
		$this->display('about');
		exit;
		}
		
		$this->display(); 
		
		 
		
		}
		
		}	
 	
	    public function ditu(){
		if($this->_get('member_id')){
 		$db= M('Vcard');
 		$info = $db->where(array('member_id'=>$this->_get('member_id')))->find();
		$this->assign('info',$info);
	    $this->display();	
			
			}	
	
		}
	 
		
		 
		
	 
 
}